home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Demos / Extend 3.0 Demo / Demo Libraries / Demo Generic Lib / Demo Generic Lib.rsrc / MODL_23533_Equation < prev    next >
Encoding:
Text File  |  1994-06-22  |  6.6 KB  |  239 lines

  1. //    This block outputs the results of an equation.
  2. //    Copyright © 1989-1994 by Imagine That, Inc.
  3. //    All Rights Reserved.
  4. //    Extend Generic Library; Bob Diamond 1/92
  5. //
  6. // modified:    7/17/92    BD - fixed StrPart index at start of string
  7. //                1/17/94 JSL added tempSeed and mySeed
  8. //                2/16/94 DJK added trace and report
  9. //                3/10/94 DJK added block label to report & trace
  10.  
  11.  
  12. real    timeArray[];
  13. Integer    dynArrayFunc[];
  14. String    oldEquation, oldLabels;
  15. integer    mySeed, tempSeed;
  16.  
  17. // This message occurs for each step in the simulation.
  18. on simulate
  19. {
  20.     // save and restore a global and local seed, 
  21.     // in case the equation contains random numbers
  22.  
  23.     tempSeed = RandomGetSeed();
  24.     RandomSetSeed(mySeed);
  25.     ConOut = EquationCalculate(con1in, con2in, con3in, con4in, con5in, 0.0, 0.0,  
  26.                                 0.0, 0.0, 0.0, dynArrayFunc);
  27.      mySeed = RandomGetSeed();
  28.      RandomSetSeed(tempSeed);
  29.      
  30.          ** sysglobal2 is the file reference number for the DEBUG TRACE
  31.     if( sysGlobal2 != 0.0 ) ** 0 is error, check for open file for TRACE
  32.         {
  33. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  34.         fileWrite(sysGlobal2,"Equation                     block number "+(MyBlockNumber())+".  Current Time:"+currentTime+".","",True);
  35.         if(getBlockLabel(myBlockNumber()) != "")
  36.             fileWrite(sysGlobal2,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  37.         fileWrite(sysGlobal2,"     "+label1+" = "+con1In,"",True);
  38.         fileWrite(sysGlobal2,"     "+label2+" = "+con2In,"",True);
  39.         fileWrite(sysGlobal2,"     "+label3+" = "+con3In,"",True);
  40.         fileWrite(sysGlobal2,"     "+label4+" = "+con4In,"",True);
  41.         fileWrite(sysGlobal2,"     "+label5+" = "+con5In,"",True);
  42.         fileWrite(sysGlobal2,"     Output = "+conOut,"",True);
  43.         fileWrite(sysGlobal2," ","",True);
  44.         }
  45.  
  46. }
  47.  
  48.  
  49. Procedure
  50. CompileIfNecessary()
  51. {
  52. String    newStr;
  53. integer err, sizeOfCompiled;
  54.  
  55. sizeOfCompiled = GetDimension(dynArrayFunc);
  56.  
  57. newStr = label1+label2+label3+label4+label5+Output;
  58.  
  59. // check to see if user has changed any equation related fields
  60. if (sizeOfCompiled == 0 || newStr != oldLabels || equation != oldEquation)
  61.     {
  62.     err = EquationCompile(label1, label2, label3, label4, label5, "", "", 
  63.                         "", "", "", Output, Equation, 8, dynArrayFunc);
  64.     
  65.     if (err)
  66.         {
  67.         oldLabels = "";        // error, force recompile
  68.         oldEquation = "";    // error, force recompile
  69.         
  70.         abort;            // don't close the dialog box
  71.         }
  72.     }
  73.     
  74.                         // good compile
  75. oldLabels = newStr;        // save changed strings for later comparison
  76. oldEquation = equation;
  77. }
  78.  
  79.  
  80. // this function returns TRUE if it finds the label in the equation
  81. integer
  82. FindInEquation(string label)
  83. {
  84. integer    index;
  85. string    beginStr, endStr, remainingStr;
  86.  
  87. if (label == "")
  88.     return(FALSE);    // empty string, return not found
  89.     
  90. remainingStr = equation;
  91.  
  92. while (TRUE)
  93.     {
  94.     index = strfind(remainingStr, label, false, false);
  95.     
  96.     if (index >= 0)    // label was found
  97.         {
  98.         // test for delimiter at beginning and end
  99.         if (index == 0)            // special case at beginning of str
  100.             beginStr = "";        // simulate delimiter
  101.         else
  102.             beginStr = strpart(remainingStr, index-1, 1);
  103.             
  104.         endStr = strpart(remainingStr, index+strlen(label), 1);
  105.         
  106.         // test for delimiter. If found, return TRUE
  107.         if ((beginStr < "A" || beginStr > "Z") && 
  108.                 (beginStr < "a" || beginStr > "z") && 
  109.                 (beginStr < "0" || beginStr > "9") && 
  110.                 beginStr != "_" && 
  111.                 (endStr < "A" || endStr > "Z") && 
  112.                 (endStr < "a" || endStr > "z") && 
  113.                 (endStr < "0" || endStr > "9") && 
  114.                                     endStr != "_")
  115.             return(TRUE);
  116.         }
  117.     else
  118.         return(FALSE);
  119.         
  120.     remainingStr = StrPart(remainingStr, index+1, 255);
  121.     }
  122.     
  123. return(FALSE);    // not found
  124. }
  125.  
  126.  
  127.  
  128. ** If the dialog data is inconsistent for simulation, abort.
  129. on ok
  130. {
  131. CompileIfNecessary();    // check if compile is needed here
  132. }
  133.  
  134. on checkData
  135. {
  136. String    errorStr;
  137.  
  138. errorStr = "";
  139.  
  140. CompileIfNecessary();    // check if compile is needed here
  141.  
  142. // now check the connections
  143. // if a label is used in equation, check the input for connected
  144.  
  145. if (FindInEquation(label1) && !Con1In) // con1in isn't connected but var is used
  146.     errorStr = "Input1";
  147. else if (FindInEquation(label2) && !Con2In)    // con2in isn't connected but var is used
  148.     errorStr = "Input2";
  149. else if (FindInEquation(label3) && !Con3In)    // con3in isn't connected but var is used
  150.     errorStr = "Input3";
  151. else if (FindInEquation(label4) && !Con4In)    // con4in isn't connected but var is used
  152.     errorStr = "Input4";
  153. else if (FindInEquation(label5) && !Con5In)    // con5in isn't connected but var is used
  154.     errorStr = "Input5";
  155.  
  156. if (errorStr != "")                    // if error
  157.     {
  158.     usererror(errorStr + " isn't connected and it is used in the equation \
  159. in Equation block number "+MyBlockNumber());
  160.  
  161.     abort;    // cancel simulation
  162.     }
  163.  
  164. // now check for connections that are not used in the equation
  165. if (!FindInEquation(label1) && Con1In) // con1in is connected but var not used
  166.     errorStr = "Input1";
  167. else if (!FindInEquation(label2) && Con2In)    // con2in is connected but var not used
  168.     errorStr = "Input2";
  169. else if (!FindInEquation(label3) && Con3In)    // con3in is connected but var not used
  170.     errorStr = "Input3";
  171. else if (!FindInEquation(label4) && Con4In)    // con4in is connected but var not used
  172.     errorStr = "Input4";
  173. else if (!FindInEquation(label5) && Con5In)    // con5in is connected but var not used
  174.     errorStr = "Input5";
  175.  
  176. if (errorStr != "")                    // if error
  177.     {
  178.     usererror(errorStr + " is connected but it isn't used in the equation \
  179. in Equation block number "+MyBlockNumber());
  180.  
  181.     abort;    // cancel simulation
  182.     }
  183. }
  184.  
  185. ** Initialize data for block
  186. on CreateBlock
  187. {
  188. label1 = "Var1";
  189. label2 = "Var2";
  190. label3 = "Var3";
  191. label4 = "Var4";
  192. label5 = "Var5";
  193. Output = "Result";
  194. equation = "Result = Var1 + Var2 + Var3 + Var4 + Var5;";
  195. oldEquation = "";
  196. oldLabels = "";
  197. }
  198.  
  199.  
  200. ** User clicked the dialog Functions button.
  201. on FunctionsAlpha
  202. {
  203. showFunctionHelp(TRUE);
  204. }
  205.  
  206.  
  207. ** User clicked the dialog Functions button.
  208. on FunctionsType
  209. {
  210. showFunctionHelp(FALSE);
  211. }
  212.  
  213.  
  214. on initSim
  215. {
  216.     if (randomSeed == 0)
  217.         mySeed = RandomGetSeed() + myBlockNumber()+1;
  218.     else
  219.         mySeed = randomSeed + myBlockNumber()+1;
  220.  
  221.     if( getPassedArray(sysGlobal0, timeArray) )
  222.         getSimulateMsgs(FALSE);
  223. }
  224.  
  225. on endSim
  226. {
  227.     ** sysGlobal1 is the file reference number for the TEXT REPORT
  228.     if( sysGlobal1 != 0.0 )  ** 0 is error, check for open file for REPORT
  229.         {
  230. // template for report:      |BLOCK NAME *****************|block number |BLOCK NUMBER*******
  231.         fileWrite(sysGlobal1,"Equation                     block number "+MyBlockNumber(),"",True);
  232.         if(getBlockLabel(myBlockNumber()) != "")
  233.             fileWrite(sysGlobal1,"Block Label: "+getBlockLabel(myBlockNumber()),"",True);
  234.         fileWrite(sysGlobal1,Equation,"",True);        
  235.         if( comments != "" )
  236.             fileWrite(sysGlobal1,"     Comments = "+comments,"",True);        
  237.         fileWrite(sysGlobal1," ","",True);
  238.         }
  239. }